//
//  RankViewController.swift
//  ShareMusic
//
//  Created by 胡岩 on 16/5/3.
//  Copyright © 2016年 胡岩. All rights reserved.
//

import UIKit

class RankViewController: SMBViewController,UITableViewDelegate,UITableViewDataSource {
    var tableView:UITableView?
    var lists:NSMutableArray = NSMutableArray.init(capacity: 0)
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(true, animated: animated)
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        self.automaticallyAdjustsScrollViewInsets = false
        self.network()
    }
    
    func network() {
        Network.networkGet(URLs.rankUrl) { (json) in
            let array:NSArray = json["data"] as! NSArray
            for dic in array {
                let model:RankModel = RankModel.init(dic: dic as! [String : AnyObject])
                self.lists.addObject(model)
            }
            self.initView()
        }
    }
    
    func initView() {
        self.tableView = UITableView.init(frame: self.view.bounds, style: UITableViewStyle.Plain)
        self.view.addSubview(self.tableView!)
        self.tableView?.delegate = self
        self.tableView?.dataSource = self
        self.tableView?.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
        let swipe:UISwipeGestureRecognizer = UISwipeGestureRecognizer.init(target: self, action: #selector(self.swipeLeft(_:)))
        swipe.direction = UISwipeGestureRecognizerDirection.Left
        self.tableView?.addGestureRecognizer(swipe)
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let model:RankModel = lists[indexPath.section] as! RankModel
        let array:NSArray = model.array!
        let dataModel:RankDataModel = array[indexPath.row] as! RankDataModel
        let songs:SongsViewController = SongsViewController()
        songs.type = SongListsType.Rank
        songs.tag_id = dataModel.tag_id
        songs.hidesBottomBarWhenPushed = true
        songs.title = model.tag_type_name
        self.navigationController?.pushViewController(songs, animated: true)
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
        let model:RankModel = lists[indexPath.section] as! RankModel
        let array:NSArray = model.array!
        let dataModel:RankDataModel = array[indexPath.row] as! RankDataModel
        cell.textLabel?.text = dataModel.tag_name
        return cell
    }
    
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 40
    }
    
    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 50
    }
    
    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        let model:RankModel = lists[section] as! RankModel
        return model.tag_type_name
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let model:RankModel = lists[section] as! RankModel
        return model.array!.count
    }
    
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return lists.count
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
